{
 "metadata": {
  "name": "op4_demo"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "import os\nfrom pyNastran.utils import print_bad_path\nfrom pyNastran.op4.op4 import OP4\nfrom numpy import float32, float64, int32, int64, product",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# define the OP4 object\nop4 = OP4()\n\n# print the docstring\n#op4.read_op4?\nhelp(op4.read_op4)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Help on method read_op4 in module pyNastran.op4.op4:\n\nread_op4(self, op4_filename=None, matrix_names=None, precision='default') method of pyNastran.op4.op4.OP4 instance\n    Reads a NASTRAN OUTPUT4 file, regular or sparse, and stores the\n    matrices as the output arguments of the function.  The number of matrices\n    read is defined by the list matrix_names.  By default, all matrices will\n    be read.  The resulting output is a dictionary of matrices that are\n    accessed by their name.\n    \n    >>> from pyNastran.op4.op4 import OP4\n    >>> op4 = OP4()\n    \n    # get all the matrices\n    >>> matrices = op4.read_op4(op4_name)\n    >>> (formA,A) = matrices['A']\n    >>> (formB,B) = matrices['B']\n    >>> (formC,C) = matrices['C']\n    \n    # or to reduce memory usage\n    >>> matrices = op4.read_op4(op4_filename, matrix_names=['A','B'])\n    >>> (formA,A) = matrices['A']\n    >>> (formB,B) = matrices['B']\n    \n    # or because you only want A\n    >>> matrices = op4.read_op4(op4_filename, matrix_names='A')\n    >>> (formA,A) = matrices['A']\n    >>>\n    :param op4_filename: an OP4 filename.  Type=STRING.\n    :param matrix_names: matrix name(s) (or None); Type=LIST OF STRINGS / STRING / NONE.\n    :param precision: specifies if the matrices are in single or double precsion\n           (values='default', 'single', 'double') which means the format will be whatever the file is in\n    \n    :returns: dictionary of matrices where the key is the name and the\n              value is [form, matrix]\n          ==== ===============\n          Form Definition\n          ==== ===============\n          1.   Square\n          2.   Rectangular\n          3.   Diagonal\n          6.   Symmetric\n          8.   Id entity\n          9.   Pseudoidentity\n          ==== ===============\n    \n        Matrix:\n          Dense Type:  NUMPY.NDARRAY\n          Sparse Type: SCIPY.SPARSE.COO_MATRIX\n    \n    .. note:: based off the MATLAB code SAVEOP4 developed by ATA-E and later UCSD.\n    .. note:: it's strongly recommended that you convert sparse matrices to another\n              format before doing math on them.  This is standard with sparse matrices.\n    \n    .. warning:: sparse binary is buggy right now\n\n"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# read the op4, will pop open a dialog box\n#matrices = op4.read_op4()\n\n#print matrices.keys()\n#key = 'CMPLX'\n#print matrices[key]",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "op4_filename = r'C:\\Users\\Steve\\Desktop\\ISat_Launch_Sm_4pt.op4'\nassert os.path.exists(op4_filename), print_bad_path(op4_filename)\n\n#specify the file\nmatrices = op4.read_op4(op4_filename)",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# more ways to read a matrix\n\n# only 1 matrix\nmatrices = op4.read_op4(op4_filename, matrix_names='FLAMA')\n\n# 1 or more matrices\nmatrices = op4.read_op4(op4_filename, matrix_names=['FLAMA','UGEXT'])",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "# extract a matrix\nform, flama = matrices['FLAMA']\nprint \"form =\", form\nprint \"type =\", type(flama)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "form = 2\ntype = <type 'numpy.ndarray'>\n"
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "from pyNastran.utils import object_methods\nprint \"keys =\", matrices.keys()\n\nprint \"object_methods\", object_methods(op4)\n#op4.getTypeNWV?",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "keys = ['FLAMA', 'UGEXT']\nobject_methods ['getMatrixInfo', 'getStartEndRow', 'getTypeNWV', 'get_dtype', 'get_markers', 'readComplexBinary', 'readOP4', 'readOP4Ascii', 'readOP4Binary', 'readRealBinary', 'read_complex_ascii', 'read_op4', 'read_op4_ascii', 'read_op4_binary', 'read_real_ascii', 'read_start_marker', 'writeDenseMatrixAscii', 'writeDenseMatrixBinary', 'writeMatrixAscii', 'writeSparseMatrixAscii']\n"
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "print matrices.keys()\nform_flama, flama = matrices['FLAMA']\nprint \"shape = \", flama.shape\nprint \"flamat nvals =\", product(flama.shape)\n\nform_ugext, ugext = matrices['UGEXT']\nprint \"form_ugext=%s type=%s\" % (form_ugext, type(ugext[0,0]))\n#print \"ugext\", ugext\nprint \"ugext.shape =\", ugext.shape\nprint \"ugext nvals =\", product(ugext.shape)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "['FLAMA', 'UGEXT']\nshape =  (3L, 167L)\nflamat nvals = 501\nform_ugext=2 type=<type 'numpy.float64'>\nugext.shape = (32274L, 167L)\nugext nvals = 5389758\n"
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "print ugext[:,:]\n#print flama",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "[[  5.54786314e-03   4.67132200e-06   1.81611022e-04 ...,  -1.03689794e-01\n   -6.91892403e-02  -1.90394624e-02]\n [ -2.13307730e-04   5.69870309e-03  -2.39269066e-02 ...,  -1.05003620e-02\n    5.25207357e-02   1.18703316e-01]\n [  8.46918619e-04   1.51207978e-03  -7.03846348e-03 ...,   2.62573796e-01\n    2.14101361e-01  -1.47248166e-01]\n ..., \n [ -3.00586525e-07   5.47611012e-05  -6.34316759e-04 ...,  -8.22188210e-03\n    2.78876443e-02  -2.64457431e-02]\n [ -1.72292481e-06   1.27842144e-06   1.80504761e-06 ...,   4.86644931e-03\n    4.63877458e-03   6.87163374e-03]\n [  7.27118310e-06   3.39441454e-06   2.71670733e-06 ...,   7.77213405e-03\n    7.15967895e-03   8.94169988e-03]]\n"
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": "#del matrices, op4",
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    }
   ],
   "metadata": {}
  }
 ]
}